package com.siwei.mes.service.sync.erp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.service.sync.erp.ErpCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 客户信息表(ErpCustomer)表服务实现类
 *
 * @author linzi
 * @since 2024-07-25 15:26:43
 */
@Slf4j
@Service
public class ErpCustomerServiceImpl implements ErpCustomerService {
    @Resource
    private ErpCustomerMapper erpCustomerMapper;

    @Override
    public ErpCustomer update(ErpCustomer entity) {
        ErpCustomer erpCustomer = erpCustomerMapper.selectByPrimaryKey(entity.getId());
        assertTrue(erpCustomer == null, "客户信息表信息不存在");
        entity.setUpdateTime(new Date());
        erpCustomerMapper.updateByPrimaryKeySelective(entity);
        return erpCustomerMapper.loadById(entity.getId());
    }

    @Override
    public PageInfo<ErpCustomer> getPage(PageQuery<ErpCustomer, ErpCustomer> query) {
        ErpCustomer searchParam = query.getParams();
        PageInfo<ErpCustomer> pageInfo = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> erpCustomerMapper.getList(searchParam));
        for (ErpCustomer erpCustomer : pageInfo.getList()) {
            String customerNo = erpCustomer.getCustomerNo();
            try {
                if (StringUtils.isNotBlank(customerNo)) {
                    double num = Double.parseDouble(customerNo);
                    erpCustomer.setCustomerNo(String.valueOf((int) num));
                }
            } catch (Exception e) {
                log.error("客户信息表获取客户编号失败", e);
            }
        }
        return pageInfo;
    }

    @Override
    public ErpCustomer loadById(Long id) {
        return erpCustomerMapper.loadById(id);
    }

    @Override
    public List<ErpCustomer> getAll(ErpCustomer entity) {
        return erpCustomerMapper.getList(entity);
    }

    @Override
    public ErpCustomer saveOrUpdateByErp(String fHtdw) {
        ErpCustomer erpCustomer = erpCustomerMapper.selectByCustomerName(fHtdw);
        if (erpCustomer == null) {
            erpCustomer = new ErpCustomer();
            erpCustomer.setId(null);
            erpCustomer.setCustomerName(fHtdw);
            erpCustomer.setCustomerNo(this.getCustomerNoStr());
            erpCustomer.setCreateTime(new Date()).setUpdateTime(new Date()).setIsDel(0);
            erpCustomerMapper.insertSelective(erpCustomer);
        }
        return erpCustomer;
    }

    /**
     * 获取客户编号
     *
     * @return
     */
    private String getCustomerNoStr() {
        String customerNoStr = erpCustomerMapper.getCustomerNoStr();
        if (StringUtils.isBlank(customerNoStr)) {
            return "1";
        }
        return customerNoStr;
    }
}
